# coding=utf-8
import time

import requests
import datetime
from pymongo import MongoClient
import sys
import json

reload(sys)
sys.setdefaultencoding('utf-8')

mgClient = MongoClient('121.40.54.235', 27017,
                       username='root2',
                       password='Dream2015',
                       authSource="stockDataStore",
                       authMechanism='SCRAM-SHA-1')
db = mgClient['stockDataStore']

'''
stocks = db['stock'].find({"code": {"$regex": "^60"}}, projection={"code": 1, "name": 1}, sort=[("code", 1)]);

for stock in stocks:
    print stock['code']

starttime = datetime.datetime.now()


stocks = db['balance_sheet'].aggregate([
    {"$match": {"code": {"$regex": "2$"}}} ,
    {"$group": {"_id": '$code', "max": { "$max": "$报表日期"}}}
])
'''

'''
specificPeriodRecord = db['stock_period_indices'].find({'$or': [
    {'code':'600009', 'period': '20171231'},
    {'code':'000021', 'period': '20171231'},
    {'code': '000022', 'period': '20171231'},
    {'code': '000021', 'period': '20171231'},
    {'code': '002021', 'period': '20171231'},
    {'code': '000021', 'period': '20171231'},
    {'code': '000021', 'period': '20171231'},
    {'code': '001021', 'period': '20171231'},
    {'code': '000021', 'period': '20171231'},
    {'code': '000321', 'period': '20171231'},
    {'code': '000021', 'period': '20171231'},
    {'code': '004021', 'period': '20171231'},
    {'code':'000021', 'period': '20171231'},
    {'code': '300022', 'period': '20171231'},
    {'code': '300021', 'period': '20171231'},
    {'code': '302021', 'period': '20171231'},
    {'code': '300021', 'period': '20171231'},
    {'code': '300021', 'period': '20171231'},
    {'code': '301021', 'period': '20171231'},
    {'code': '300021', 'period': '20171231'},
    {'code': '300321', 'period': '20171231'},
    {'code': '300021', 'period': '20171231'},
    {'code': '304021', 'period': '20171231'},
    {'code':'600010', 'period': '20171231'}]}, projection={'roe': "1", 'roa': 1, 'npr': 1});

for record in specificPeriodRecord:
    print record

stocks = db['stock_period_indices'].aggregate([
     {"$match": {'$or': [
     {'$and': [{"roe": {"$gt": 0.2}}, {'period': '20171231'}]},
     {'$and': [{"roe": {"$gt": 0.2}}, {'period': '20161231'}]},
     {'$and': [{"roe": {"$gt": 0.2}}, {'period': '20151231'}]}
     ]}} ,
     {"$group": {"_id": '$code', "count": { "$sum": 1}}},
     {"$match": {"count": {"$gt": 2}}}
     ])
'''
#for stock in stocks:
#    print stock

'''
latestRecords = db['stock_daily_k'].aggregate([
    {'$match': {'$or': [
        {'code': '600004'},
        {'code': '600009'},
        {'code': '300004'},
        {'code': '000009'},
        {'code': '600004'},
        {'code': '000001'},
        {'code': '000004'},
        {'code': '000009'},
        {'code': '600004'},
        {'code': '600009'},
        {'code': '300004'},
        {'code': '000009'},
        {'code': '300003'}

    ]}},
    {'$sort': {'code': -1, 'day': -1}},
    {'$group': {
        '_id': '$code',
        'pe_ttm': {'$first': '$pe_ttm'},
        'pe_koufei_ttm': {'$first': '$pe_koufei_ttm'},
        'day': {'$first': '$day'}
    }}
])
'''


latestRecords = db['stock_top_shareholder'].aggregate([
    {'$match': {'name': '香港中央结算(代理人)有限公司'}},
    {'$sort': {'code': -1, 'period': -1}},
    {'$group': {
        '_id': '$code',
        'num': {'$first': '$num'},
        'period': {'$first': '$period'}
    }}
])

for record in latestRecords:
    print record
